package com.ruoyi.aitwsk.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.IAitEntityInfoService;
import com.ruoyi.aitcommon.utils.EntityCache;
import com.ruoyi.aitcommon.utils.ExceptionUtils;
import com.ruoyi.aitcommon.utils.JSONResult;
import com.ruoyi.aitcommon.utils.ListMapUtils;
import com.ruoyi.aitcommon.utils.LogUtils;
import com.ruoyi.aitcommon.utils.RestServiceUtils;

import cn.hutool.core.util.StrUtil;

/**
 * 威斯克LMIS接口封装
 * @author Administrator
 *
 */
@Service
public class WSKLIMSRestService {
	@Autowired
	IAitEntityInfoService infoService;
	@Autowired
	EntityCache entityCache;

	List<Map<String,Object>> filterlist;
	
	/**
	 * 查询服务
	 * @param context
	 * @param url
	 * @param query
	 * @return
	 * @throws Exception
	 */
	public List<Map<String,Object>> query(Map<String,Object>context, String url
			,Map<String,Object> query,Map<String,Object> keyMap) throws Exception {
		
		ExceptionUtils.checkBlankOrUndefined(context, "context为空");
		ExceptionUtils.checkBlankOrUndefined(context.get("entity"), "context.entity为空");
		
//		调用LIMS OPENAPI
		JSONObject retjson = RestServiceUtils.getJSON(url,null,query);
		
		// 处理LIMS服务返回
		if (!retjson.containsKey("success"))
			throw new Exception("返回数据错误,无[success]!");
		if (!retjson.getBoolean("success")) {
			String msg = retjson.getString("message");
			throw new Exception("LIMS调用报错:" + msg);
		}
		String entity=context.get("entity").toString();
		JSONArray retarr = retjson.getJSONArray(entity);
		List<Map<String, Object>> retlist = ListMapUtils.array2Map(retarr);
//		构造Key字段
		ListMapUtils.putKey(retlist, keyMap);
		return retlist;
	}
	
	/**
	 * 通用Rest调用
	 * @param url
	 * @param data
	 * @return
	 * @throws Exception
	 */
	private JSONObject restInvoke(String url, Map<String,Object> data)
			throws Exception {
//		构造调用参数
		
//		调用LIMS OPENAPI
		JSONObject retjson = RestServiceUtils.post(url,null,new JSONObject(data));

		// 处理LIMS服务返回
		if (!retjson.containsKey("success"))
			throw new Exception("返回数据错误,无[success]!");
		if (!retjson.getBoolean("success")) {
			String msg = retjson.getString("message");
			throw new Exception("LIMS调用报错:" + msg);
		}
		return retjson;
	}
	
	/**
	 * 单据保存服务
	 * @param context
	 * @param url
	 * @param datas
	 * @param entityInfo
	 * @return
	 * @throws Exception
	 */
	@ServiceLog("LIMS单据保存服务")
	public List<JSONObject> docSave(Map<String, Object> context,String url, List<Map<String, Object>> datas,
			JSONObject entityInfo) throws Exception {
		List<JSONObject> retlist = new ArrayList<JSONObject>();
		
		ExceptionUtils.checkBlankOrUndefined(context, "context为空");
		ExceptionUtils.checkBlankOrUndefined(url, "url为空");
		ExceptionUtils.checkBlankOrUndefined(entityInfo, "entityInfo为空");
		
//		预处理
		PreDeal(context, datas, entityInfo);

		for (Map<String,Object> data : filterlist) {
//			循环单条处理
			try {
//				构造接口数据
				
//				调用接口
				JSONObject retjson = restInvoke(url, data);
//				处理实体日志和缓存
				infoService.insertAitEntityInfo(entityInfo, data, null);
//				处理返回
				retlist.add(retjson);
			} catch (Exception ex) {
//				处理实体日志和缓存-失败信息
				infoService.insertAitEntityInfo(entityInfo, data, ex);
//				处理返回
				retlist.add(JSONResult.error(ex.getMessage()));
			}
		}
		return retlist;
	}
	
	/**
	 * 单据保存前处理
	 * @param context
	 * @param datas
	 * @param entityInfo
	 * @throws Exception
	 */
	private void PreDeal(Map<String, Object> context,List<Map<String, Object>> datas, JSONObject entityInfo)
			throws Exception {
		ExceptionUtils.checkBlankOrUndefined(context.get("fenge"), "context.fenge为空");
		ExceptionUtils.checkBlankOrUndefined(context.get("zuhe"), "context.zuhe为空");
		
//		构造U9形式的ListMap
		JSONArray fenge=(JSONArray)context.get("fenge");
		List<Map<String, Object>> fengelist = ListMapUtils.array2Map(fenge);
		List<Map<String, Object>> retlist = ListMapUtils.transU9Map(datas, fengelist, context.get("zuhe").toString());
//		从缓存过滤重复数据
		this.filterlist=entityCache.filterEntityChange(entityInfo, retlist);
		
	}
}
